<?php
/**
 * Api控制器基类
 *
 * @author andery
 */

namespace Common\Controller;

use Common\Controller\BaseController;

class ApiController extends BaseController
{
    protected $appkey = 'wz8ry0xu';//加密密钥
    protected $appsecret = 'f053fae8-3dee-11eb-8098-00e0b60d654c';
    protected $userinfo = array();

    public function _initialize()
    {
        parent::_initialize();

        //网站状态
        if (C('qscms_isclose')) {
            header('Content-Type:text/html; charset=utf-8');
            exit('网站关闭:' . C('qscms_close_reason'));
        }

        if ((!in_array(CONTROLLER_NAME, array('Members')) && !in_array(ACTION_NAME, array('axc','get_remittance_notice','get_baozhengjin_notice','get_gcbbuy_notice','get_zzfw_notice'))) || ACTION_NAME == 'getUserInfo') {

            $token = $_SERVER['HTTP_TOKEN'];    //获取headers里的token信息
            if (empty($token)) {
                $this->ajaxReturn(2, 'token信息为空不能访问');
            }
            $reslut = openssl_decrypt($token, 'AES-128-CBC', $this->appsecret, 0, '');

            if (empty($reslut)) {
                $this->ajaxReturn(2, '验证token信息失败');
            }
            $reslut = json_decode($reslut, true);
            if (empty($reslut['uid'] || empty($reslut['appkey']) || empty($reslut['appsecret']) || empty($reslut['10bzzw']))) {
                $this->ajaxReturn(2, '验证信息失败');
            }
            $stoken = S('uid_' . $reslut['uid']);
            if (empty($stoken)) {
                $this->ajaxReturn(11, 'token信息过期');
            }
            if ($stoken <> $token) {
                $this->ajaxReturn(2, 'token信息不匹配');
            }

            $userinfo = M('members')->where(['uid' => $reslut['uid']])->find();
            if ($this->appkey <> $reslut['appkey'] || $this->appsecret <> $reslut['appsecret'] || empty($userinfo)) {
                $this->ajaxReturn(1, '个人信息匹配错误');
            }
            if($userinfo['avatars']) $userinfo['avatars'] = attach($userinfo['avatars'], 'avatar');
            $this->userinfo = $userinfo;
            C('visitor', $userinfo);
            //重置用户缓存信息
            S('uid_' . $reslut['uid'], $stoken, 86400 * 2);

        }


    }


    /**
     * 新注册用户名生成
     */
    protected function newName($data)
    {
        srand((double)microtime() * 1000000);
        $rand = rand(100000, 999999);
        $data['username'] = strtolower(C('qscms_reg_prefix')) . substr($data['mobile'], -4) . $rand;
        $exist_num = D('Members')->where(array('username' => $data['username']))->count();
        if ($exist_num > 0) {
            do {
                $data['username'] = strtolower(C('qscms_reg_prefix')) . $data['mobile'] . strtolower(get_rand_char(2));
                $exist_num = D('Members')->where(array('username' => $data['username']))->count();
            } while ($exist_num > 0);
        }
        return $data;
    }

    /**
     * [_password 微信注册用户密码生成]
     */
    public function newPassword($username)
    {
        if (C('qscms_reg_password_tpye') == 1) {
            $password = $username;
        } elseif (C('qscms_reg_password_tpye') == 2) {
            $password = D('Members')->randstr();
        } else {
            $password = C('qscms_reg_weixin_password');
        }
        return $password;
    }

    /**
     * 本地用户添加
     */
    protected function _local_add($add_data)
    {
        $user_mod = D('Members');
        if (false !== $user_mod->create($add_data)) {
            $user_mod->password = $user_mod->make_md5_pwd($add_data['password'], $user_mod->pwd_hash);
            $user_mod->invitation_code = $user_mod->randstr(8, true);
            $user_mod->reg_source = C('LOG_SOURCE') ? C('LOG_SOURCE') : 1;
            $user_mod->reg_source_cn = '小程序';
            if (!$uid = $user_mod->add()) {
                $this->_error = $user_mod->getError();
                return false;
            } else {
                //写入会员日志
                write_members_log(array('uid' => $uid, 'utype' => $add_data['utype'], 'username' => $add_data['username']), 'register', '用户注册');
                $add_data['uid'] = $uid;
                return D('Members')->where(['uid' => $uid])->find();
            }
        }
        $this->_error = $user_mod->getError();
        return false;
    }

    /**
     * 获取人才顾问等级
     */
    protected function level()
    {
        $level = M('GuwenLevel')->field('logo,name,id')->select();
        $result = [];
        foreach ($level as $value) {
            if ($value['id'] < 3) {
                $value['click_status'] = 0;
            } else {
                $value['click_status'] = 1;
            }
            $result[] = $value;
        }
        return $result;
    }
    protected function level_api()
    {
        $level = M('GuwenLevel')->field('logo,name as categoryname,id')->select();
        $result = [];
        foreach ($level as $value) {
            if ($value['id'] < 3) {
                $value['click_status'] = 0;
            } else {
                $value['click_status'] = 1;
            }
            $result[] = $value;
        }
        return $result;
    }

    /**
     * 获取所属行业
     */
    public function get_trade_list()
    {
//        $tag_classify_class = new \Common\qscmstag\classifyTag(array('列表名' => 'trade_list', '类型' => 'QS_trade', '显示数目' => '100', 'cache' => '0', 'type' => 'run',));
//        $trade_list = $tag_classify_class->run();
//        $trade_lst = [];
        $trade_list=  D('CategoryMajor')->get_major_list();
        foreach ($trade_list as $k => $v) {
            //var_dump($v);die;
            $trade_lst[$k]['id'] = $k;
            $trade_lst[$k]['value'] = $v['categoryname'];
        }
        $trade_list = array_values($trade_lst);
        return $trade_list;
    }

    /**
     * 获取所属行业
     */
    public function get_trade_list_api()
    {
        $trade_list=  D('CategoryMajor')->get_major_list();
        $trade_lst = [];
//        foreach ($trade_list as $k=>$v){
//            $trade_lst[$k]['id']=$k;
//            $trade_lst[$k]['value']=$v;
//        }
        //   $trade_list=array_values($trade_lst);
        return $trade_list;
    }

    /**
     * 获取企业规模
     */
    public function get_scale_list()
    {
        $tag_classify_class = new \Common\qscmstag\classifyTag(array('列表名' => 'scale_list', '类型' => 'QS_scale', '显示数目' => '100', 'cache' => '0', 'type' => 'run',));
        $scale_list = $tag_classify_class->run();
        $frontend = new \Common\Controller\FrontendController;
        $scale_lst = [];
        foreach ($scale_list as $k => $v) {
            $scale_lst[$k]['id'] = $k;
            $scale_lst[$k]['value'] = $v;
        }
        $scale_list = array_values($scale_lst);
        return $scale_list;
    }

    /**
     * 获取工作性质
     */
    public function get_nature_list()
    {
        //工作性质
        $tag_classify_class = new \Common\qscmstag\classifyTag(array('列表名' => 'nature_list', '类型' => 'QS_jobs_nature', '显示数目' => '100', 'cache' => '0', 'type' => 'run',));
        $nature_list = $tag_classify_class->run();
        $nature_lst = [];
        foreach ($nature_list as $k => $v) {
            $nature_lst[$k]['id'] = $k;
            $nature_lst[$k]['value'] = $v;
        }
        $nature_list = array_values($nature_lst);

        return $nature_list;
    }

    /**
     * 获取学历要求
     */
    public function get_education_list()
    {
        //学历要求
        $tag_classify_class = new \Common\qscmstag\classifyTag(array('列表名' => 'education_list', '类型' => 'QS_education', '显示数目' => '100', 'cache' => '0', 'type' => 'run',));
        $education_list = $tag_classify_class->run();
        $education_lst = [];
        foreach ($education_list as $k => $v) {
            $education_lst[$k]['id'] = $k;
            $education_lst[$k]['value'] = $v;
        }
        $education_list = array_values($education_lst);
        return $education_list;

    }

    /**
     * 获取工作经验
     */
    public function get_experience_list()
    {
        $tag_classify_class = new \Common\qscmstag\classifyTag(array('列表名' => 'experience_list', '类型' => 'QS_experience', '显示数目' => '100', 'cache' => '0', 'type' => 'run',));
        $experience_list = $tag_classify_class->run();
        $experience_lst = [];
        foreach ($experience_list as $k => $v) {
            $experience_lst[$k]['id'] = $k;
            $experience_lst[$k]['value'] = $v;
        }
        $experience_list = array_values($experience_lst);
        return $experience_list;
    }


    /**
     * [isCompany 检测企业信息是否存在]
     * @return boolean [false]
     */
    protected function isCompany()
    {
        if (!$company = M('CompanyProfile')->where(['uid' => $this->userinfo['uid']])->find()) return false;

        return $company;
    }

    /**
     * [isResume 检测简历是否存在]
     * @return boolean [false]
     */
    protected function isResume()
    {
        if (!$reg = M('Resume')->where(['uid' => $this->userinfo['uid']])->find()) return false;

        return $reg;
    }

    /**
     * 获取发布的招聘类型
     * @return mixed
     */

    public function get_type_list()
    {
        if (false === $list = F('category_pin_list')) {
            $list = M('category_pin')->field('id,categoryname as value')->where(['display' => 1])->order('category_order desc')->select();
            F('category_pin_list', $list);
        }
        return $list;
    }
    public function get_type_list_copy()
    {
        if (false === $list = F('category_pin_list')) {
            $list = M('category_pin')->field('id,categoryname as value')->where(['display' => 1,'id'=>['not in',[5,6]]])->order('category_order desc')->select();
            F('category_pin_list', $list);
        }
        return $list;
    }
    /**
     * 获取简历下载的价格
     * @return mixed
     */
    protected function get_jl_download_price()

    {
        $jl_down_money = M('config')->where(['name' => 'jl_download_money'])->getField('value');

        return $jl_down_money;
    }

    /**
     * 获取域名
     * @return mixed
     */
    protected function get_site_url()
    {
        $value=M('config')->where(['name'=>'site_domain'])->getField('value');
        return $value;
    }

    /**
     * 判断简历是否下载
     * @param $resume_id
     * @param $company_uid
     * @return mixed
     */
    public function check_down_resume($resume_id,$company_uid){
        $where['resume_uid'] = $resume_id;
        $where['company_uid'] = $company_uid;
//var_dump(M('company_down_resume')->where($where)->find());
//var_dump(M('company_down_resume')->getLastSql());
//die;
        return M('company_down_resume')->where($where)->find()?1:0;
    }

    /**
     * 到岗时间 列表
     */
   public function get_dao_gang_list()
   {
     $list=  D('Category')->category_dao_time();
     $_list=[];
     foreach ($list as $k=>$v)
     {
         $_list[$k]['id'] = $k;
         $_list[$k]['value'] = $v;
     }

     return array_values($_list);

   }


}